package com.ellwood.common.core.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ellwood.common.core.utils.EllStrUtil;

/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.common.core.base
 * @ClassName: ServiceFactoryImpl
 * @Description: // service工厂接口实现基类
 * @Author: Ellwood
 * @CreateDate: 2024/7/13 - 上午12:36
 * @Version: V1.0.0
 */
public abstract class ServiceFactoryImpl<M extends MapperFactory<T>, T> extends ServiceImpl<M, T> implements ServiceFactory<T> {

    /**
     * 获取分页对象
     *
     * @param pageBase 分页阐述
     * @return 返回分页结果
     */
    protected IPage<T> getPage(PageBase pageBase) {
        Page<T> page = new Page<>(pageBase.getPage(), pageBase.getLimit());

        // 排序
        if (EllStrUtil.isNotBlank(pageBase.getOrder())) {
            if (pageBase.isAsc()) {
                return page.addOrder(OrderItem.asc(pageBase.getOrder()));
            } else {
                return page.addOrder(OrderItem.desc(pageBase.getOrder()));
            }
        }
        return page;
    }

}
